package com.sc.web.gwt.client.view.uicomponent;


import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.*;

import java.util.HashMap;
import java.util.Map;

public abstract class CollapsibleListWidget<T extends Widget> extends Composite {
    @UiField
    InlineLabel titleLabel;

    @UiField
    FlowPanel flowPanel;

    protected Map<String, T> widgetList = new HashMap<String, T>();

    public interface Binder extends UiBinder<HTMLPanel, CollapsibleListWidget> {
    }

    public CollapsibleListWidget() {
        Binder binder = GWT.create(Binder.class);
        initWidget(binder.createAndBindUi(this));
    }

    public void setTitleText(String title) {
        titleLabel.setText(title);
    }

    public void setWidget(String name, String displayName) {
        if (widgetList.containsKey(name)) {
            return;
        }
        final T checkBox = createWidget(name, displayName);
        widgetList.put(name, checkBox);
        flowPanel.add(checkBox);
    }

    protected abstract T createWidget(final String name, final String displayName);
}